<?php
namespace app\index\controller;
use app\index\controller\Base;
//use think\Request;
use think\Db;
class  Link extends Base{
	//短链接
	public  function shorturl(){
		$id = input('id');
		$sp_id = input('sp_id');
	    $uid =Db::table('think_collect')->where('id','EQ',$id)->value('uid');
		$pid = Db::table('think_pid')->where('uid','EQ',$uid)->where('status','EQ',1)->field('pid')->find();
		$session = Db::table('think_user')->where('id','EQ',$uid)->value('session_key');
		$pid = $pid['pid'];
		if($pid){
			if($session){
				$ch = curl_init();
	     		$url = "http://api.tkurl.top/privilege?type=1&appkey=VYnPTEkn&id=".$sp_id."&pid=".$pid."&session=".$session;
				curl_setopt ($ch, CURLOPT_URL, $url);
				curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
				$dxycontent = curl_exec($ch);
				curl_close($ch);
				$data = json_decode ($dxycontent,true);
				if(array_key_exists('tbk_privilege_get_response',$data)){
					$url = $data['tbk_privilege_get_response']['result']['data']['coupon_click_url'];
					$url_new="http://api.tkurl.top/spread?appkey=VYnPTEkn&url=".$url;
                    $res= file_get_contents($url_new);
					$data_new = json_decode ($res,true);
					return $data_new;
				}else{
					$data['code']=2;
					if($data['error_response']['code']==27){
					$re = Db::table('think_user')->where('id','EQ',$uid)->update(['session_key'=>'','up_session_time'=>'']);	
					  $data['msg']='授权过期,请重新授权';	
					}else{
					  $data['msg']='该商品链接失效，请反馈消息给放单人';	
					}
			      
					return $data;
				}
			}else{
				$data['code']=0;
			    $data['msg']='请授权，绑定session';
				return $data;
			}
		}else{
			$data['code']=0;
			$data['msg']='请绑定PID';
			return $data;
		}
	} 
 
	//淘口令
	public  function ambush(){
		$id = input('id');
		$sp_id = input('sp_id');
		$goods_id =input('goods_id');
	    $uid =Db::table('think_collect')->where('id','EQ',$id)->value('uid');
		$pid = Db::table('think_pid')->where('uid','EQ',$uid)->where('status','EQ',1)->field('pid')->find();
		$session = Db::table('think_user')->where('id','EQ',$uid)->value('session_key');
		$pid = $pid['pid'];
		if($pid){
			if($session){
				$ch = curl_init();
	     		$url = "http://api.tkurl.top/privilege?type=1&appkey=VYnPTEkn&id=".$sp_id."&pid=".$pid."&session=".$session;
				curl_setopt ($ch, CURLOPT_URL, $url);
				curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
				$dxycontent = curl_exec($ch);
				curl_close($ch);
				$data = json_decode ($dxycontent,true);
				if(array_key_exists('tbk_privilege_get_response',$data)){
				    $arr = Db::table('think_goods')->where('id','EQ',$goods_id)->field('title,large_img')->find();
					$url = $data['tbk_privilege_get_response']['result']['data']['coupon_click_url'];
					$text = urlencode($arr['title']);
					$url_new="http://api.tkurl.top/Kl_Create?appkey=VYnPTEkn&text=".$text."&url=".$url."&logo=".$arr['large_img'];
                    $res= file_get_contents($url_new);
					$data_new = json_decode ($res,true);
					return $data_new;
				}else{
					$data['code']=2;
					if($data['error_response']['code']==27){
					$re = Db::table('think_user')->where('id','EQ',$uid)->update(['session_key'=>'','up_session_time'=>'']);	
					  $data['msg']='授权过期,请重新授权';	
					}else{
					  $data['msg']='该商品链接失效，请反馈消息给放单人';	
					}
					return $data;
				}
			}else{
				$data['code']=0;
			    $data['msg']='请授权，绑定session';
				return $data;
			}
		}else{
			$data['code']=0;
			$data['msg']='请绑定PID';
			return $data;
		}
	} 
	
	
	
	
}
